package vfc.views.results
{
	import assets.Assets;
	
	import flash.events.MouseEvent;
	
	import mate.events.SiteEvent;
	
	import mx.containers.VBox;
	import mx.controls.Image;
	import mx.controls.Label;
	import mx.controls.Text;
	
	import vfc.model.valueObjects.Site;

	public class ListItemBase extends VBox
	{
		
		[Bindable] public static var SOUPBOWL_SMALL:Class = Assets.SOUPBOWL_SMALL;
		
		public var image:Image;
		public var title:Label;
		public var address:Text;
		
		protected var _index:int;
		protected var _site:Site;
		protected var _selected:Boolean;
		private var dataChanged:Boolean;
		private var selectedChanged:Boolean;
		
		public function ListItemBase()
		{
			super();
		}
		
		override protected function childrenCreated():void{
			super.childrenCreated();
			
			addEventListener(MouseEvent.CLICK,onClick);
			addEventListener(MouseEvent.MOUSE_OVER,onMouseOver);
			addEventListener(MouseEvent.MOUSE_OUT,onMouseOut);
		}
		
		override protected function commitProperties():void{
			super.commitProperties();
			if(dataChanged){
				dataChanged = false;
				title.text = _site.name;
				//TODO - SPLIT THE DATA
				address.text = _site.address;
			}
			if(selectedChanged){
				selectedChanged = false;
				if(_selected)
					setStyle("backgroundColor",getStyle("selectedColor"));
				else
					setStyle("backgroundColor",getStyle("visitedColor"));
			}
		}
		
		public function set selected(value:Boolean):void{
			_selected = value;
			selectedChanged = true;
			invalidateProperties();
		}
		
		public function get selected():Boolean{
			return _selected;
		}

		private function onClick(event:MouseEvent):void{
			dispatchEvent(new SiteEvent(SiteEvent.SITE_SELECTED,site));	
		}
		
		private function onMouseOver(event:MouseEvent):void{
			setStyle("backgroundColor",getStyle("rollOverColor"));
		}
		
		private function onMouseOut(event:MouseEvent):void{
			if(_selected)
				setStyle("backgroundColor",getStyle("selectedColor"));
			else
				setStyle("backgroundColor",getStyle("upColor"));
		}

		public function get site():Site {
			return _site;
		}

		public function set site(o:Site):void {
			_site = o;
			dataChanged = true;
			invalidateProperties();
		}
		
		public function get index():int {
			return _index;
		}

		public function set index(o:int):void {
			_index = o;
		}

		
	}
}